package util.webSearch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.LinkedList;

import org.apache.http.HttpException;

import util.io.URLInput;

import util.parser.ParseGoogleSuggestions;

public class GoogleQuery {

	// Put your website here
	private static final String HTTP_REFERER = "http://www.example11.com/";

	public static LinkedList<String> getRelatedSearchesUDF(String query,
			String path, long delay) throws IOException, URISyntaxException  {

	
		LinkedList<String> set = new LinkedList<String>();

		String post = null;

		// post = URLInput.readFromURL(url);
		try {

			Thread.sleep(delay);
			
			String url = "http://google.com/complete/search?q="
				+ URLEncoder.encode(query, "utf8") + "&output=toolbar";

			post = URLInput.submitURLRequestUDF(url, path);
			
						
			if(post == null  || (post.equals(""))){
				
				
				return null;
			}
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println("Error getting suggestions of query " + query);
			System.out.println(e.getMessage());
			return set;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			
			System.out.println("Error getting suggestions of query:" + query);
			System.out.println(e.getMessage());
			return set;
		} 

		set = ParseGoogleSuggestions.suggestions(query, post);

		return set;
	}

}